package com.example.venue_reservation_service.controller;

import com.example.venue_reservation_service.domain.User;
import com.example.venue_reservation_service.dto.*;
import com.example.venue_reservation_service.mq.DelayMessageSender;
import com.example.venue_reservation_service.service.UserService;
import com.example.venue_reservation_service.vo.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.time.LocalTime;

@RestController
@RequestMapping("/user")
@Tag(name = "用户接口管理", description = "用户模块相关接口，包含登录、信息管理、充值等功能")
@CrossOrigin
public class UserController {

    @Resource
    private UserService userService;

    @Autowired
    private DelayMessageSender delayMessageSender;

    // 原始预约处理方法替换为发送延迟消息
    public void scheduleReservationCheck(long reservationId, LocalDate reservationDate, LocalTime end) {
        delayMessageSender.sendReservationCheck(reservationId, reservationDate, end);
    }

    @GetMapping("/test")
    @Operation(
            summary = "示例接口",
            description = "用于测试系统通信的示例接口，发送测试消息到RabbitMQ",
            parameters = {}
    )
    public Result test() {
        delayMessageSender.sendTestMessage();
        return Result.ok("测试消息已发送，请观察控制台输出").message("success");
    }

    @Operation(
            summary = "用户登录",
            description = "通过微信小程序临时授权码获取用户OpenID完成登录",
            parameters = {
                    @Parameter(
                            name = "code",
                            description = "微信小程序登录时获取的临时授权码（有效期5分钟）",
                            required = true,
                            schema = @Schema(type = "string"),
                            example = "0A3m7r000aBcDe123f456"
                    )
            }
    )
    @GetMapping("/login")
    public Result getOpenId(@RequestParam("code") String code) {
        return userService.getOpenId(code);
    }

    @Operation(
            summary = "账号密码登录",
            description = "用户、管理员以及超级管理员登录系统，验证账号密码",
            parameters = {
                    @Parameter(
                            name = "dto",
                            description = "登录DTO，包含账号和密码",
                            required = true,
                            schema = @Schema(implementation = AdminDTO.class)
                    )
            }
    )
    @PostMapping("/admin")
    public Result adminLogin(@RequestBody AdminDTO dto) {
        return userService.adminLogin(dto);
    }

    @Operation(
            summary = "完善用户信息",
            description = "用户完善个人信息，如姓名、性别、生日等",
            parameters = {
                    @Parameter(
                            name = "userInfo",
                            description = "用户信息DTO，包含用户基本资料",
                            required = true,
                            schema = @Schema(implementation = UserInfo.class)
                    )
            }
    )
    @PostMapping("/update")
    public Result update(@RequestBody UserInfo userInfo) {
        return userService.userUpdate(userInfo);
    }

    @Operation(
            summary = "查询用户列表",
            description = "根据条件查询用户列表，支持分页和筛选",
            parameters = {
                    @Parameter(
                            name = "dto",
                            description = "用户查询DTO，包含查询条件和分页参数",
                            required = true,
                            schema = @Schema(implementation = UserDTO.class)
                    )
            }
    )
    @PostMapping("/list")
    public Result queryUserList(@RequestBody UserDTO dto) {
        return userService.getUserList(dto);
    }


    @Operation(
            summary = "获取用户认证信息",
            description = "根据用户ID获取其认证信息（如学校、专业等）",
            parameters = {
                    @Parameter(
                            name = "userId",
                            description = "用户ID",
                            required = true,
                            schema = @Schema(type = "integer")
                    )
            }
    )
    @GetMapping("/detail")
    public Result get(@RequestParam("id") Integer userId) {
        return userService.getSchool(userId);
    }

    @Operation(
            summary = "用户充值",
            description = "用户账户余额充值",
            parameters = {
                    @Parameter(
                            name = "userId",
                            description = "用户ID",
                            required = true,
                            schema = @Schema(type = "integer")
                    ),
                    @Parameter(
                            name = "amount",
                            description = "充值金额（单位：元）",
                            required = true,
                            schema = @Schema(type = "number", format = "double")
                    )
            }
    )
    @PostMapping("/recharge")
    public Result recharge(@RequestParam("id") Integer userId, @RequestParam("amount") Double amount) {
        return userService.userRecharge(userId, amount);
    }

    @Operation(
            summary = "补充联系方式",
            description = "用户补充手机号和邮箱信息",
            parameters = {
                    @Parameter(
                            name = "dto",
                            description = "联系方式DTO，包含手机号和邮箱",
                            required = true,
                            schema = @Schema(implementation = PhoneDTO.class)
                    )
            }
    )
    @PostMapping("/phone")
    public Result check(@RequestBody PhoneDTO dto) {
        return userService.checkPhone(dto);
    }

    @Operation(
            summary = "获取用户数量",
            description = "获取系统用户总数",
            parameters = {}
    )
    @GetMapping("/num")
    public Result num() {
        return userService.useNum();
    }

    @Operation(
            summary = "查询管理员信息",
            description = "查询管理员列表，支持分页查询",
            parameters = {
                    @Parameter(
                            name = "dto",
                            description = "分页查询DTO，包含页码和每页数量",
                            required = true,
                            schema = @Schema(implementation = PageDTO.class)
                    )
            }
    )
    @PostMapping("/admininfo")
    public Result admininfo(@RequestBody PageDTO dto) {
        return userService.admininfo(dto);
    }

    @Operation(
            summary = "创建管理员",
            description = "超级管理员创建新的管理员账号",
            parameters = {
                    @Parameter(
                            name = "user",
                            description = "管理员用户对象，包含账号信息",
                            required = true,
                            schema = @Schema(implementation = User.class)
                    )
            }
    )
    @PostMapping("/create")
    public Result create(@RequestBody User user) {
        return userService.createAdmin(user);
    }

    @Operation(
            summary = "移除管理员",
            description = "超级管理员移除指定管理员账号",
            parameters = {
                    @Parameter(
                            name = "userId",
                            description = "待移除的管理员用户ID",
                            required = true,
                            schema = @Schema(type = "integer")
                    )
            }
    )
    @GetMapping("/remove")
    public Result remove(@RequestParam("userId") Integer userId) {
        return userService.removeAdmin(userId);
    }

    @Operation(
            summary = "修改密码",
            description = "用户修改账号密码",
            parameters = {
                    @Parameter(
                            name = "dto",
                            description = "密码修改DTO，包含原密码和新密码",
                            required = true,
                            schema = @Schema(implementation = PasswdDTO.class)
                    )
            }
    )
    @PostMapping("/passwd")
    public Result passwd(@RequestBody PasswdDTO dto) {
        return userService.passwd(dto);
    }

    @Operation(
            summary = "上传头像",
            description = "用户上传个人头像",
            parameters = {
                    @Parameter(
                            name = "userId",
                            description = "用户ID",
                            required = true,
                            schema = @Schema(type = "integer")
                    ),
                    @Parameter(
                            name = "image",
                            description = "头像图片文件（支持JPG、PNG格式）",
                            required = true,
                            schema = @Schema(type = "file")
                    )
            }
    )
    @PostMapping("/upload")
    public Result upload(@RequestParam("id") Integer userId, @RequestParam("image") MultipartFile file) {
        return userService.upload(userId, file);
    }

    @Operation(
            summary = "获取用户信息",
            description = "根据用户ID获取详细信息",
            parameters = {
                    @Parameter(
                            name = "userId",
                            description = "用户ID",
                            required = true,
                            schema = @Schema(type = "integer")
                    )
            }
    )
    @GetMapping("/info/{id}")
    public Result info(@PathVariable("id") Integer userId) {
        return userService.getUserInfo(userId);
    }

    @ApiOperation("基于qq邮箱重置密码")
    @PostMapping("/reset")
    public Result reset(@RequestBody ResetDTO dto){
        return userService.passwordReset(dto);
    }
}